import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 1.安装VueRouter
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'index',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/layout'),
    redirect: '/first',
    children: [
      {
        path: '/first',
        name: 'first',
        component: () => import('@/views/firstPage'),
        meta: {
          title: '看板'
        }
      },
      {
        path: '/second',
        name: 'second',
        component: () => import('@/views/second'),
        meta: {
          title: '运营分类'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login')
  }
]

// 3.创建路由对象
const router = new VueRouter({
  mode: 'hash',
  routes
})

router.beforeEach((to, from, next) => {
  console.log(store.state.auth.token, to)
  if (to.path !== '/login') {
    if (!store.state.auth.token) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

// 4.导出
export default router
